﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdCreateViewVertical.h"

#include "ICamera.h"
#include "ICanvas.h"
#include "IDocument.h"
#include "IMainWindow.h"
#include "IModelView.h"
#include "IUiDocument.h"
#include "IUiView.h"
#include "IUiViewMouseEvent.h"
#include "IUserTransaction.h"
#include "UiDocumentViewUtils.h"
#include "Vector3dUtils.h"
#include "CommandIds.h"
#include "ModelViewUtils.h"
#include "CommandRegister.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

REGISTER_COMMAND(CmdCreateViewVertical);
CmdCreateViewVertical::CmdCreateViewVertical()
    :gcmp::CommandBase(COMMAND_VIEW_VERTICAL)
{}

bool CmdCreateViewVertical::IsEnabled() const
{
    IDocument* pCurrentDoc = UiDocumentViewUtils::GetCurrentDocument();

    return  (pCurrentDoc == nullptr) ? false : true;
}

gcmp::OwnerPtr<gcmp::IAction> CmdCreateViewVertical::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IUiDocument*  pUiDocument = UiDocumentViewUtils::GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocument, L"请打开文档", L"GDMP", L"2023-09-30");

    std::wstring viewName = L"ViewXZ";
    IUiView* pUiViewXZ = CreateUiView(pUiDocument, L"ViewXZ");
    if(pUiViewXZ)
    {
        pUiViewXZ->SetTitle(pUiDocument->GetDbDocument()->GetName() + L" - 立面");
#if 0
        pUiViewXZ->GetUiViewMouseEvent()->Delete(ApplicationEventBus::Get());
        pUiViewXZ->GetUiViewMouseEvent()->Add(ApplicationEventBus::Get());
#endif
        pUiViewXZ->GetCanvas()->MarkNeedRefreshByViewScale(true);
        pUiViewXZ->GetCanvas()->MarkNeedRefreshLineTypeScale(true);
    }
    return gcmp::OwnerPtr<gcmp::IAction>(nullptr);
}

gcmp::IModelView* CmdCreateViewVertical::CreateModelView(gcmp::IDocument* pDocument, const std::wstring viewName)
{
    gcmp::IModelView* pModelViewXZ = nullptr;

    if (nullptr == pDocument)
    {
        return pModelViewXZ;
    }
    //查找
    for each (gcmp::IModelView* pModelView in gcmp::IModelView::GetAllModelViews(pDocument))
    {
        if (gcmp::BuiltInViewType::TwoDimensional == pModelView->GetViewType())
        {
            if (gcmp::Vector3dUtils::IsEqual(Vector3d(1, 0, 0), pModelView->GetRightDirection()) && gcmp::Vector3dUtils::IsEqual(Vector3d(0, 0, 1), pModelView->GetUpDirection()))
            {
                if (pModelView->GetName() == viewName)
                {
                    pModelViewXZ = pModelView;
                    break;
                }
            }
        }
    }
    //创建
    if (nullptr == pModelViewXZ)
    {
        if (gcmp::IUserTransaction::IsInUserTransaction(pDocument, nullptr))
        {
            pModelViewXZ = gcmp::IModelView::Create(pDocument, viewName, gcmp::BuiltInViewType::TwoDimensional, gcmp::Vector3d(0, 1, 0), gcmp::Vector3d::Zero, gcmp::Vector3d(1, 0, 0), gcmp::Vector3d(0, 0, 1), gcmp::ElementCreationOptions::Normal);
            if (nullptr != pModelViewXZ)
            {
                pModelViewXZ->SetUserDataId(SectionViewType);
            }
        }

        OwnerPtr<IUserTransaction> opUserTransaction = gcmp::IUserTransaction::Create(pDocument, L"创建2D模型视图");
        pModelViewXZ = gcmp::IModelView::Create(pDocument, viewName, gcmp::BuiltInViewType::TwoDimensional, gcmp::Vector3d(0, 1, 0), gcmp::Vector3d::Zero, gcmp::Vector3d(1, 0, 0), gcmp::Vector3d(0, 0, 1), gcmp::ElementCreationOptions::Normal);
        if (nullptr == pModelViewXZ)
        {
            opUserTransaction->Rollback();
            return nullptr;
        }
        pModelViewXZ->SetUserDataId(SectionViewType);
        pModelViewXZ->SetViewScale(10.0);
        pModelViewXZ->SetDetailLevel(ModelViewDetailLevel::Fine);
        opUserTransaction->Commit();
    }

    return pModelViewXZ;
}

gcmp::IUiView* CmdCreateViewVertical::CreateUiView(IUiDocument* pUiDocument, const std::wstring viewName)
{
    IUiView* pUiViewXZ = nullptr;
    //1.检查打开的视图是否有2D视图
    for each (gcmp::IUiView* pUiView in pUiDocument->GetAllUiViews())
    {
        if (pUiView->IsSubUiView())
        {
            continue;
        }

        if (gcmp::BuiltInViewType::TwoDimensional != pUiView->GetModelView()->GetViewType())
        {
            continue;
        }

        gcmp::IModelView* pModelView2d = pUiView->GetModelView();
        if (!(gcmp::Vector3dUtils::IsEqual(Vector3d(1, 0, 0), pModelView2d->GetRightDirection()) && gcmp::Vector3dUtils::IsEqual(Vector3d(0, 0, 1), pModelView2d->GetUpDirection())))
        {
            continue;
        }
        if (pUiView->GetTag() == viewName)
        {
            pUiViewXZ = pUiView;
            break;
        }
    }
    //2.有的话直接打开
    if (pUiViewXZ != nullptr)
    {
        if (UiDocumentViewUtils::GetCurrentUiView() != pUiViewXZ)
        {
            IMainWindow::GetMainWindow()->CreateOrOpenUiView(pUiDocument->GetId(), pUiViewXZ->GetModelView()->GetElementId(), false, pUiViewXZ->GetTag());
        }
        else
        {
            pUiViewXZ->GetCanvas()->Refresh();
        }

        return pUiViewXZ;
    }
    //3.没有的话继续检查文档是否有2D模型
    if (pUiDocument->GetDbDocument() == nullptr)
    {
        return nullptr;
    }
    IModelView* pModelViewXZ = CreateModelView(pUiDocument->GetDbDocument(), L"ModelView2d_XZ");
    if (nullptr == pModelViewXZ)
    {
        return nullptr;
    }

    return gcmp::IMainWindow::GetMainWindow()->CreateOrOpenUiView(pUiDocument->GetId(), pModelViewXZ->GetElementId(), false, viewName);
}
